<?php
/**
 * Created by PhpStorm.
 * User: jinqiang
 * Date: 2019/02/04
 * Time: 11:48
 */

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;
use App\Http\Services\System\UserService;
use Illuminate\Http\Request;

class UserController extends Controller
{
    //获取用户信息
    public function user(Request $request, UserService $userService)
    {
        $user = $request->user();
        $user = $userService->getUserInfo($user);
        return success('用户信息', $user);
    }

    //获取用户列表
    public function list(Request $request, UserService $userService)
    {
        $this->validate($request,[
            'page' => 'required',
            'limit' => 'required'
        ]);
        $param = $request->all();
        $res = $userService->search($param);
        return result($res);
    }

    //获取飞书用户列表
    public function fsList(Request $request, UserService $userService)
    {
        $roles = explode(",", auth()->guard('api')->user()->role);
        if(!in_array("admin", $roles)){
            return error("抱歉, 您还不具备该操作权限");
        }
        $this->validate($request,[
            'page' => 'required',
            'limit' => 'required',
            'subject' => 'required|in:kz,mz,yx,xm,js,qyx'
        ]);
        $param = $request->all();
        $res = $userService->searchFS($param);
        return result($res);
    }

    //获取工厂用户列表
    public function gcList(Request $request, UserService $userService)
    {
        $roles = explode(",", auth()->guard('api')->user()->role);
        if(!in_array("admin", $roles)){
            return error("抱歉, 您还不具备该操作权限");
        }
        $this->validate($request,[
            'page' => 'required',
            'limit' => 'required',
        ]);
        $param = $request->all();
        $res = $userService->searchGC($param);
        return result($res);
    }

    //用户修改
    public function set(Request $request, UserService $userService)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $param = $request->all();
        $id = $param['id'];
        $data = [];
        if($param['role'] ?? []){
            $data['role'] = implode(",", $param['role']);
        }
        if(isset($param['phone'])){
            $data['phone'] = $param['phone'];
            $data['password'] = bcrypt($param['phone']);
        }
        if(isset($param['status'])){
            $data['status'] = (Int)$param['status'];
        }
        $res = $userService->setUser($id, $data);
        return $res['code'] === 0 ? success("用户设置成功") : error("用户设置失败", $res);
    }

    //用户修改--工厂
    public function gcSet(Request $request, UserService $userService)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $param = $request->all();
        $id = $param['id'];
        if(isset($param['status'])){
            $data['status'] = (Int)$param['status'];
        }
        $res = $userService->setGcUser($id, $data);
        return $res['code'] === 0 ? success("用户设置成功") : error("用户设置失败", $res);
    }

    //用户新增
    public function add(Request $request, UserService $userService)
    {
        $this->validate($request, [
            'role' => 'required',
            'name' => 'required',
            'phone' => 'required',
            'source_from' => 'required|in:fs,gc,visitor'//目前仅支持 游客
        ]);
        $param = $request->all();
        $res = $userService->addUser($param);
        return $res['code'] === 0 ? success("用户新增成功") : error("用户新增失败", $res);
    }

    //用户新增---工厂
    public function gcAdd(Request $request, UserService $userService)
    {
        $this->validate($request, [
            'factory_name' => 'required',
            'supplier_id' => 'required',
            'name' => 'required',
            'phone' => 'required',
        ]);
        $param = $request->all();
        $res = $userService->addGcUser($param);
        return $res['code'] === 0 ? success("用户新增成功") : error("用户新增失败", $res);
    }

    //获取用户飞书主体
    public function subjects()
    {
        $configs = array_column(config('scm.subject'), null, 'is_subject');
        $subjects = [];
        foreach ($configs as $key => $item){
            $subjects[$key] = ['label' => $item['label']];
        }
        return success("飞书主体", $subjects);
    }

    //用户同步到nnyy_user
    public function sync(Request $request, UserService $userService)
    {
        $this->validate($request, [
            'id' => 'required',
            'subject' => 'required|in:kz,mz,xm,yx,js,qyx'
        ]);
        $param = $request->all();
        $res = $userService->sync($param);
        return $res['code'] === 0 ? success("用户同步成功") : error("用户同步失败", $res);
    }

    //切换主体
    public function changeSubject(Request $request, UserService $userService)
    {
        $this->validate($request, [
            'subject' => 'required|in:kz,mz,xm,yx,js,qyx'
        ]);
        $param = $request->all();
        $res = $userService->changeSubject($param['subject']);
        return $res['code'] === 0 ? success("切换成功") : error($res['msg'], $res);
    }
}
